package org.yimon.tool.algo.baidu;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author yimon.gao
 * @Description 删除字符串中连续的重复的字符，删除之后组成新的字符串继续删除重复字符
 * @Date 2024/10/21 16:37
 */
public class RemoveDuplicates {

    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();


    public static void main(String[] args) {
        threadLocal.set("aa");
        threadLocal.get();
        threadLocal.remove();
        System.out.println(RemoveDuplicates.removeDuplicates("abbaca"));
    }

    public static String removeDuplicates(String s) {
        //特殊情况判读那
        if(s == null || s.length() <= 1) {
            return s;
        }
        Set<Character> noDup = new LinkedHashSet<>();
        int start = 0;
        for (start = 0; start < s.length(); start++) {
            if(!noDup.add(s.charAt(start))) {
                noDup.remove(s.charAt(start));
            }
        }
        StringBuilder sb = new StringBuilder();
        Iterator<Character> iterator = noDup.iterator();
        while(iterator.hasNext()) {
            sb.append(iterator.next());
        }
        return sb.toString();
    }
}
